﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

//@author A0083726Y
namespace EZXPNS
{
    
    public partial class CategoryForm : Form
    {
        IncomeForm categoryform;
        public CategoryForm(IncomeForm frm)
        {
            InitializeComponent();
            categoryform = frm;
        }
        
        private void CategoryForm_Load(object sender, EventArgs e)
        {
        
            StreamReader inputfile;
            inputfile = File.OpenText("../../IncomeCategory.txt");

            while (inputfile.EndOfStream == false)
            {
                string tempName = inputfile.ReadLine();

                ListViewItem lvi = new ListViewItem(tempName);
                listView1.Items.Add(lvi);
            }
            inputfile.Close();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("Nothing is entered!");
                return;
            }
            else
            {
                IncomeCategory.IncomeItems.Add(textBox1.Text);

                //Refresh data storage
                IncomeCategory.UpdateCategory();

                ListViewItem lvi = new ListViewItem(textBox1.Text);
                listView1.Items.Add(lvi);

                //Update IncomeForm's Category_comboBox;
                categoryform.Category_comboBox.Items.Add(textBox1.Text);

                MessageBox.Show("Category added successfully!");

                textBox1.Text = "";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("No item is selected!");
                return;
            }
            else
            {
                //Update IncomeCategory list
                IncomeCategory.IncomeItems.RemoveAt(listView1.SelectedItems[0].Index);

                //Refresh data storage
                IncomeCategory.UpdateCategory();

                //Update IncomeForm's Category_comboBox;
                categoryform.Category_comboBox.Items.RemoveAt(listView1.SelectedItems[0].Index);

                //Refresh listView1
                listView1.SelectedItems[0].Remove();

                MessageBox.Show("Category deleted successfully!");
            }
        }

    }
}
